Imports System.Data.SqlClient

Friend Class DBScanView
    Inherits DBBase

    Private moServer As SQLAutoDocLib.Server
    Private moDatabase As SQLAutoDocLib.Database

    Public Event ItemScanStart(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long, ByVal lViewID As Long)
    Public Event ItemScanComplete(ByVal lSessionID As Long, ByVal lViewID As Long, ByVal oEndDate As Date)

    Public Sub New( _
                ByVal oServer As SQLAutoDocLib.Server, _
                ByVal oDatabase As SQLAutoDocLib.Database)

        moServer = oServer
        moDatabase = oDatabase
    End Sub

    Public Function Scan(ByVal lSessionID As Long) As Boolean
        Dim bChangesFound As Boolean = False

        Dim oFactory As New View_Factory
        Dim oAllViews As Collection = oFactory.GetAllViewsForDatabase(moDatabase.ID)

        For Each oView As SQLAutoDocLib.View In oAllViews
            oView.IsDeleted = True
            oView.Save()
        Next

        Dim iAlteredViews As Integer = 0
        Dim bViewsAddedOrUpdated As Boolean = False
        RefreshViewsForDatabase(lSessionID, oAllViews, iAlteredViews)
        If iAlteredViews > 0 Then bViewsAddedOrUpdated = True

        If bViewsAddedOrUpdated = False Then
            '---Were any views deleted?
            For Each oView As SQLAutoDocLib.View In oAllViews
                If oView.IsDeleted = True Then
                    oView.DatabaseRevID = moDatabase.RevID
                    oView.Save(bForceAdd:=True)
                    bChangesFound = True
                End If
            Next
        Else
            bChangesFound = True
        End If

        Return bChangesFound
    End Function

    Private Sub RefreshViewsForDatabase( _
                    ByVal lSessionID As Long, _
                    ByRef oAllViews As Collection, _
                    ByRef iAlteredViews As Integer)

        Dim sSQL = "select distinct"
        sSQL = sSQL & "     a.id,"
        sSQL = sSQL & "     b.name 'ViewOwner',"
        sSQL = sSQL & "     a.name 'ViewName'"
        sSQL = sSQL & " from "
        sSQL = sSQL & " dbo.sysobjects a"
        sSQL = sSQL & " join dbo.sysusers b on b.uid=a.uid"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.type = 'v'"
        sSQL = sSQL & "     and a.category!=2"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL, moDatabase.DecryptedConnectionString)

        Dim oLoginFactory As New Login_Factory
        Dim oViewFactory As New View_Factory

        Dim bViewAdded As Boolean = False

        For Each oRow As DataRow In oData.Rows
            Dim oLogin As SQLAutoDocLib.Login = oLoginFactory.GetLoginByName(moDatabase.ID, oRow.Item("ViewOwner"))
            Dim oView As SQLAutoDocLib.View = oViewFactory.GetViewByName(moDatabase.ID, oLogin.ID, oRow.Item("ViewName"))

            If oView Is Nothing Then
                '---Found a new view
                bViewAdded = True

                oView = New SQLAutoDocLib.View
                With oView
                    .Name = oRow.Item("ViewName")
                    .OwnerID = oLogin.ID
                    .OID = oRow.Item("id")
                    .DatabaseID = moDatabase.ID
                    .DatabaseRevID = moDatabase.RevID
                End With

                iAlteredViews = iAlteredViews + 1
            Else
                bViewAdded = False
            End If

            If oAllViews.Contains(CStr(oView.ID)) Then
                oAllViews(CStr(oView.ID)).IsDeleted = False
            End If

            oView.IsDeleted = False

            oView.Save()

            RaiseEvent ItemScanStart(moDatabase.ServerID, moDatabase.ID, lsessionid, oView.ID)

            oView.DatabaseRevID = moDatabase.RevID
            Dim bViewUpdated As Boolean = oView.Scan()
            If bViewUpdated = True Then
                iAlteredViews = iAlteredViews + 1
            End If

            RaiseEvent ItemScanComplete(lSessionID, oView.ID, Now)
        Next
    End Sub
End Class
